unit HT_Weght;

interface

uses
  SysUtils;

const
  SymCr   : Char  = Char(13);
  SymLf   : Char  = Char(10);
  SymAck  : Char  = Char($06);
  SymNack : Char  = Char($15);

  htIdle        = 0;
  htCalibrate   = 1;
  htRead        = 2;

  HT_ARRAY_MAX  = 32;

type
  HtDataMess_t = record
    Polarity  : Byte;
    Data      : array[0..7] of char;
    U1        : char;
    U2        : char;
    S1        : char;
    S2        : char;
    Cr        : Byte;
    Lf        : Byte;
  end;
  HtDataMess_p = ^HtDataMess_t;

  HtCommand = record
    C1  : Char;
    C2  : Char;
    Cr  : Char;
    Lf  : Char;
  end;

  HtData_t = record
    Value     : Real;
    Stab      : Boolean;
    Precesion : Integer;
  end;

  HtAnswer1_t = record
    A1  : Char;
    A2  : Char;
    A3  : Char;
    Cr  : Char;
    Lf  : Char;
  end;
  HtAnswer1_p = ^HtAnswer1_t;

  HtAnswer2_t = record
    A1  : Char;
    Cr  : Char;
    Lf  : Char;
  end;
  HtAnswer2_p = ^HtAnswer2_t;

var
  HtStatus : Integer;
  rxHtMess : array[0..HT_ARRAY_MAX - 1] of char;
  rxHtCounter : Integer;
  CrRead : Boolean;
  pMess : HtDataMess_p;
  pAnswer1 : HtAnswer1_p;
  pAnswer2 : HtAnswer2_p;

procedure HtFillCommand(var HtComm : HtCommand; Comm : byte);
function CalcWeight(mess : HtDataMess_p) : HtData_t;

implementation

procedure HtFillCommand(var HtComm : HtCommand; Comm : byte);
begin
  HtComm.C1 := 'O';
  HtComm.C2 := Char(Comm + $30);
  HtComm.Cr := SymCr;
  HtComm.Lf := SymLf;
end;

function CalcWeight(mess : HtDataMess_p) : HtData_t;
var
  i : Integer;
  k : Byte;
  st : string;
  stab : Boolean;
begin
  st := '';
  Result.Stab := False;
  for i := 0 to 7 do begin
    st := st + mess^.Data[i];
  end;
  i := Pos('.', st);
  if i > 0 then st[i] := ',';
  Result.Precesion := 8 - i;
  try
    Result.Value := StrToFloat(st);
  except
  end;
  if mess^.S2 = 'S' then Result.Stab := True;
end;

initialization
  HtStatus := 0;
  rxHtCounter := 0;
  CrRead := False;
  pMess := Pointer(Addr(rxHtMess));
  pAnswer1 := Pointer(Addr(rxHtMess));
  pAnswer2 := Pointer(Addr(rxHtMess));
end.
